<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 发布订阅模式 --一对多的关系

        var salesOffices = {}

        // salesOffices.clientList = []

        // salesOffices.listen = function (key, fn) {
        //     if (!this.clientList[key]) {
        //         this.clientList[key] = []
        //     }
        //     this.clientList[key].push(fn)
        // }

        // salesOffices.trigger = function () {

        //     var key = Array.prototype.shift.call(arguments)

        //     fns = this.clientList[key]


        //     for (let i = 0; i < fns.length; i++) {

        //         fns[i].apply(this, arguments)

        //     }
        // }


        var Event = {
            clientList: [],
            listen: function (key, fn) {
                if (!this.clientList[key]) {
                    this.clientList[key] = []
                }
                this.clientList[key].push(fn)
            },
            trigger() {
                var key = Array.prototype.shift.call(arguments)
                fns = this.clientList[key]
                for (let i = 0; i < fns.length; i++) {
                    fns[i].apply(this, arguments)
                }
            }
        }


        
        function installEvent(target, Source) {
            for (let key in Source) {
                target[key] = Source[key]
            }

            return target
        }

        var salesOffices = installEvent(salesOffices, Event)
        salesOffices.listen("square88", function (price, squaremeter) {
            console.log('价格' + price)
            console.log('面积' + squaremeter)
        })
        salesOffices.listen("square108", function (price, squaremeter) {
            console.log('价格' + price)
            console.log('面积' + squaremeter)
        })
        salesOffices.listen("square88", function (price, squaremeter) {
            console.log('价格' + price)
            console.log('面积' + squaremeter)
        })
        salesOffices.trigger("square88", 1000, 88)
        salesOffices.trigger("square108", 1000, 108)

    </script>
</body>

</html>